<?php

/**
 * The base controller which is used by the Front and the Admin controllers
 */
class My_Controller extends CI_Controller {
function __construct()
	{
		parent::__construct();
	}
}


class Admin_Controller extends My_Controller {

	public $theme_data = array(
		'layout_main'=>'admin/include/layout',
	);
	public $mytable = "admin";
	public $heading = "";
	var $resource = "";
	var $url_search = '';
	function __construct()
	{
		parent::__construct();
		$this->load->model("mresource");
		$this->load->model("madmin");
		$this->load->model("mresourcegroups");
	}
	function nopermission(){
		 redirect(admin_url().'nopermission.html');
	}
	
	function clean_search(){
		setcookie ("search", "", time() - 3600);
		redirect($this->url_index);
	}
	
	
	function search(){
		$post = $this->input->post();
		$data = array();
		if($post){
			$s = $this->input->post('txtSearch');
			$field = $this->input->post('selFieldSearch');
			$condition = $this->input->post('selCondition');
			if(trim($s) !='')
				setcookie("search",$field."|".$s.'|'.$condition, time() + 7200);
			redirect($this->url_index);
		}
		else if($this->input->cookie('search')) {
			$str = $this->input->cookie('search');
			$str = explode("|",$str);
			if(count($str) >2){
				$field = $str[0];
				$s = $str[1];
				$condition = $str[2];
			}
			else {
				return $data;
			}
		}
			if(isset($s)){
				if($condition == '='){
					$this->db->where($field,$s);
					return $data;
				}
				else {
					$this->db->like($field,$s);
					return $data;
				}
				
			}
	 
		return $data;
	}
	
	function delete($deleteID = 0){
		$this->check_permission();
		if($deleteID > 0){
			$this->{$this->model}->delete($deleteID);
		}
	}
	function check_permission($editID = 0, $method = ''){
		if($method =='')
			$method = $this->router->fetch_method();
		if($method =='index'){
			if (!$this->permission->check("select", $this->{$this->model}->resCode))
				 return $this->nopermission();
		}
		else if($method =='delete'){
			if (!$this->permission->check("delete", $this->{$this->model}->resCode))
				die( "Bạn không có quyền này");
		}
		else {
			if($editID ==0){
				if (!$this->permission->check("insert", $this->{$this->model}->resCode))
				return $this->nopermission();
			}
			if($editID > 0){
				if (!$this->permission->check("update", $this->{$this->model}->resCode))
				return $this->nopermission();
			}
		}
	}
}
 